import wepy from 'wepy';
import tip from '@/utils/tip'
const wxRequest = async(params = {}, url) => {
    return new Promise((resolve, reject) => {
        let data = params.query || {};
        let token = 'token=' + wx.getStorageSync('USER_TOKEN');
        wx.request({
            url: url,
            method: params.method || 'GET',
            data: data,
            header: { 
                'Content-Type': 'application/json' ,
                cookie: token,
            },
            success: (res => {
                tip.loaded();
                if (res.statusCode === 200) {
                  //200: 服务端业务处理正常结束
                  resolve(res)
                } else if(res.statusCode === 401){
                    tip.fail("请先授权！")
                } else {
                  tip.loaded();
                  tip.fail("请求失败！")
                  reject(res)
                }
            }),
            fail: (res => {
                tip.loaded();
                tip.fail(res.errMsg)
                console.log('错误',res)
                reject(res)
            })
        });
    })
}

module.exports = {
    wxRequest
}
